<?php
class ml_model_dbLikeMe extends Lib_datamodel_db
{
    private $oRedis;
    const ML_LIKEME_STATUS_NORMAL = 1;
    const ML_LIKEME_STATUS_DELETE = 9;

    public function __construct()
    {
        $db_config = ml_factory::load_standard_conf('dbContent');
        parent::__construct('likeme' , $db_config['likeme']);
    }

    public function createLike($uid, $rid, $aid, $att, $from = 1, $time = null)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        $arrInsert = array(
            'uid' => $uid,
            'content_rid' => $rid,
            'act_uid' => $aid,
            'attitude' => $att,
            'status' => self::ML_LIKEME_STATUS_NORMAL,
            'from' => $from,
            'cip' => Tool_ip::get_real_ip(),
        );
        
        if (!empty($time)) {
            $arrInsert['ctime'] = $time;
        }

        return $this->insert($arrInsert);
    }

    
    /**
     *
     * 查找某内容的表态非手机主动方  wap
     * @param unknown_type $uid
     * @param unknown_type $rid
     * @param unknown_type $begin
     * @param unknown_type $num 默认20，结尾-1
     * @param unknown_type $att
     * @todo 筛选任意多状态值
     */
    public function getListNotWapByRid($oid, $rid, $begin=0 , $num=20, $att=null, &$list){
        if(!$this->init_db($oid, self::DB_SLAVE))
        return false;

        $fields = ' act_uid,attitude,ctime';
        $limit = ' ORDER BY ctime DESC LIMIT '.$begin.','.$num;
        $whereAtt = ($att === null)? ' AND attitude ='.$att: '';
        $sql = "SELECT {$fields} FROM {$this->table} WHERE content_rid = '{$rid}' AND `from` != 2 {$whereAtt} {$limit}";
        $rs = $this->fetch($sql);
        if ($rs) {
            $list = $this->_data;
            return true;
        }
        else return false;
    }

    
    /**
     *
     * 查找某内容的表态主动方
     * @param unknown_type $uid   此处的uid已不仅仅是宝贝主人，所以不应加 “ uid = oid”限制  下同
     * @param unknown_type $rid
     * @param unknown_type $begin
     * @param unknown_type $num 默认20，结尾-1
     * @param unknown_type $att
     * @todo 筛选任意多状态值
     */
    public function getListByRid($oid, $rid, $begin=0 , $num=20, $att=null, &$list){
        if(!$this->init_db($oid, self::DB_SLAVE))
        return false;

        $fields = ' act_uid,attitude,ctime';
        $limit = ' ORDER BY ctime DESC LIMIT '.$begin.','.$num;
        $whereAtt = ($att === null)? ' AND attitude ='.$att: '';
        $sql = "SELECT {$fields} FROM {$this->table} WHERE  content_rid = '{$rid}' {$whereAtt} {$limit}";
        $rs = $this->fetch($sql);
        if ($rs) {
            $list = $this->_data;
            return true;
        }
        else return false;
    }

    /**
     *上面那个用不了呀
     */
    public function getUidsByRid($oid, $rid, $att=null,&$list){
        if(!$this->init_db($oid, self::DB_SLAVE))
        return false;

        $fields = ' act_uid';
        $whereAtt = ($att === null)? ' AND attitude ='.$att: '';
        $sql = "SELECT {$fields} FROM {$this->table} WHERE  content_rid = '{$rid}' {$whereAtt}";
        $rs = $this->fetch($sql);
        if ($rs) {
            $list = $this->_data;
            return true;
        }
        else return false;
    }

    public function countContentAttUser($uid, $rid, &$count) {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $where = " content_rid= '{$this->escape($rid)}'";
        $rs = $this->fetch_count($where);
        if (!$rs) return false;
        $count = $this->_data;
        return true;
    }
    /**
     * 获取非手机版表态（手机版专用）
     *
     * @param unknown_type $uid
     * @param unknown_type $rid
     * @param unknown_type $count
     * @return unknown
     */
    
    public function countAttUserNotWap($uid, $rid, &$count) {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $where = " content_rid= '{$this->escape($rid)}' AND `from` !=2 ";
        $rs = $this->fetch_count($where);
        if (!$rs) 
            return false;
        $count = $this->_data;
        return true;
    }


    /**
     *
     * 对我表态的
     * @param unknown_type $uid
     *
     */
    public function attitudeMe($uid,$begin=0 , $num=40){

        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $uid=$this->escape($uid);
        $sql="select `act_uid`,`content_rid`,`attitude`,`ctime`,`from` from `$this->table` where `uid`='$uid' and `status`='1' order by `ctime` desc limit $begin,$num";

        $rs=$this->fetch($sql);
        return $rs?$this->_data:false;

    }
    /**
     *
     * 对我表态的总数
     * @param unknown_type $uid
     *
     */
    public function countAttMe($uid){

        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $uid=$this->escape($uid);
        $sql="select count(*) as `num` from `$this->table` where `uid`='$uid'";
        $rs=$this->fetch($sql);
        return $rs?$this->_data[0]:false;

    }

    /**
     *
     * 某内容的态度总分
     * @param unknown_type $uid 内容所有者
     * @param unknown_type $rid 内容
     */
    public function sumContentAttScore($uid, $rid, &$score){
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $sql = 'SELECT SUM(attitude) FROM '. $this->table. ' WHERE  content_rid=\''. $this->escape($rid).'\'';
        $rs = $this->fetch($sql);
        if (!$rs) return false;
        $score = $this->_data[0]['SUM(attitude)'];
        $score = $score == null ? 0: $score;
        return true;
    }

    /**
     *
     * 某用户的态度总分
     * @param unknown_type $uid
     */
    public function sumUserAttScore($uid, &$score) {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $sql = 'SELECT SUM(attitude) FROM '. $this->table. ' WHERE uid='. $uid;
        $rs = $this->fetch($sql);
        if (!$rs) return false;
        $score = $this->_data[0]['SUM(attitude)'];
        $score = $score == null ? 0: $score;
        return true;
    }
    /**
     *
     * 删除喜欢我的
     * @param unknown_type $uid
     */
    public function deleteLike($uid, $rid,$act_uid) {
        if(!$this->init_db($uid))
        return false;

        /**
         * @todo
         */
        return $this->update(array('status' => '9'), "`act_uid`='$act_uid' and `content_rid`='$rid'", 1);

    }
}